package com.bytecodezz.app.sketch.common.base

import android.widget.ImageView
import android.widget.TextView
import androidx.viewbinding.ViewBinding
import com.bytecodezz.app.base.extend.ui.onClick
import com.bytecodezz.app.base.framework.base.model.BaseModelActivity
import com.bytecodezz.app.base.framework.tools.StatusBarTools
import com.bytecodezz.app.sketch.common.R

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/5 18:05
 * @package: com.bytecodezz.app.base
 *
 * @description:业务层级的顶级Activity基类
 *
 */
abstract class BaseUIActivity<V : ViewBinding, M : BaseUIViewModel> :
    BaseModelActivity<V, M>() {
    protected var titleTv: TextView? = null
    protected var backImageView: ImageView? = null
    var statusBarColor: Int? = null

    override fun initOtherObserver() {
        super.initOtherObserver()
        viewModel.isShowProgress.observe(this) { isShowProgress ->
            if (isShowProgress) {
                //show progress
                showProgress()

            } else {
                //dismiss progress
                dismissProgress()
            }
        }
    }

    fun showProgress(content: String = "加载中", isCancel: Boolean = false) {

    }

    fun dismissProgress() {
    }

    /**
     * @author: bytecodezz
     * @version: version 1.0
     * @description:
     * @param null
     * @return null
     *
     */
    override fun onActionPostView() {
        super.onActionPostView()
        titleTv = findViewById(R.id.app_bar_title_tv)
        backImageView = findViewById(R.id.app_bar_back_imv)
        backImageView?.onClick {
            onKeyBack()
        }

        initStatusBar()
    }

    open fun getStatusBar(): Int? =
        resources.getColor(R.color.common_base_status_bar_background_color)

    /**
     * 初始化状态栏颜色， getStatusBar()方法返回系统资源定义的颜色值，也为默认值
     * 如果有自己的需求颜色，复写该getStatusBar方法返回即可，如果不想让基类设置状态栏颜色，复写getStatusBar方法返回null接口中断设置
     */
    private fun initStatusBar() {
        statusBarColor = getStatusBar()
        statusBarColor?.let {
            setStatusBarColor(it)
        }
    }

    fun setStatusBarColor(color: Int) {
        StatusBarTools.setStatusBarColor(this, color)
    }

    fun setStatusBarPadding(rootViewId: Int, isOpenPadding: Boolean = true) {
        StatusBarTools.setOffsetStatusBar(this, findViewById(rootViewId), isOpenPadding, true)
    }

    fun setStatusBarMargin(rootViewId: Int, isOpenMargin: Boolean = true) {
        StatusBarTools.setOffsetStatusBar(this, findViewById(rootViewId), isOpenMargin, false)
    }
}